package org.example.dataStructure.stack;

public class MyStack<T> {

    private Integer capacity = 16;//容量
    private Object[] stackList;//数组
    private Integer stackSize = 0;//实际存储大小

    public MyStack(){
        stackList = new Object[this.capacity];
    }

    public void push(T value){
        if((this.stackSize+(this.stackSize/2))>=capacity){
            expansion();
        }
        stackList[stackSize] = value;
        stackSize++;
    }

    public boolean is_empty(){
        if(stackSize>0) return false;
        return stackList[stackSize] == null;
    }

    private void expansion(){
        this.capacity = this.capacity*2;
        Object[] stack = new Object[this.capacity];
        if (stackSize >= 0) System.arraycopy(stackList, 0, stack, 0, stackSize);
        this.stackList = stack;
    }

    public T top(){
        return (T) stackList[0];
    }

    public T pop(){
        if(stackSize==0 && stackList[stackSize]==null){
            return null;
        }else {
            T value = (T) stackList[stackSize-1];
            stackList[stackSize-1] = null;
            stackSize--;
            return value;
        }
    }

    public Integer size(){
        return stackSize;
    }

}
